package Screens;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.amms.SoundSource3D;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VolumeControl;

import resource.HighScoreStore;
import resource.MenuScreen;
import resource.Keyboard;
import resource.SaveGame;
import resource.Sound;

import Game.CanvasGame;

/**
 * @author Hide
 *
 */
public class Menu extends Screen {
	//private MenuScreen res;
	private Image[] img;
	private SaveGame saveGame;
	VolumeControl vc;

	public Menu(CanvasGame _canvas) {
		super(_canvas);
		initMenu();
	}
	
	public boolean initMenu(){
		//res = new MenuScreen();
		img = new Image[MenuScreen.IMG_LENGHT];
		saveGame = new SaveGame();
		load_screen();
		return true;
	}
	
	public void load_screen() {
		// TODO Auto-generated method stub
		for (int i = 0; i < MenuScreen.IMG_LENGHT; i++) {
			load(i);
		}
	}
	
	
	
	public void un_load_screen() {
		// TODO Auto-generated method stub
		//img = null;
		if(img == null)    return;
        for (int i = 0; i < MenuScreen.IMG_LENGHT; i++) {
            unLoad(i);
        }
	}


	public boolean load(int i) {
		/* TODO: load the images here */
	    try {
	    	img[i] = Image.createImage(MenuScreen.imgSource[i]);
	    } catch (Exception ex) {
	    	System.err.println(ex.toString()+"aaaa");
	    	return false;
	    }
	    return true;
	}
	
	public void unLoad(int i) {
        /* TODO: make sure the object get's destroyed */
        img[i] = null;
    }

	
	public void keyEvent(int keycode, int event) {
		// TODO Auto-generated method stub
		if(event == Keyboard.KEY_EVENT_DOWN){
            switch(keycode){
                case Keyboard.KEY_UP:
                	MenuScreen.currentMenu = (MenuScreen.currentMenu - 1 +MenuScreen.MAX_MENU) % MenuScreen.MAX_MENU;
					try {
						if(canvas.getVolume()>0){
//							if(canvas.sound[Sound.BEEP].getState() == Player.STARTED){
//								canvas.sound[Sound.BEEP].stop();
//							}
							vc = (VolumeControl) canvas.sound[Sound.BEEP].getControl("VolumeControl");
							vc.setLevel(canvas.getVolume());
							canvas.sound[Sound.BEEP].start();
						}
					} catch (MediaException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                	positionY();
                    break;
                case Keyboard.KEY_DOWN:
                	MenuScreen.currentMenu = (MenuScreen.currentMenu + 1) % MenuScreen.MAX_MENU;
                	try {
                		if(canvas.getVolume()>0){
//							if(canvas.sound[Sound.BEEP].getState() == Player.STARTED){
//								canvas.sound[Sound.BEEP].stop();
//							}
							vc = (VolumeControl) canvas.sound[Sound.BEEP].getControl("VolumeControl");
							vc.setLevel(canvas.getVolume());
							canvas.sound[Sound.BEEP].start();
						}
					} catch (MediaException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                	positionY();
                    break;
                case Keyboard.KEY_OK:
                	ActionClick(MenuScreen.currentMenu);
                    break;
            }
        }
	}
	public void ActionClick(int i){
		switch (i) {
		case MenuScreen.NEWGAME:
			un_load_screen();
			saveGame.open();
			saveGame.clear();
			saveGame.close();
			canvas.setScreen(new Game(canvas));
			break;
		case MenuScreen.CONTINUE:
			saveGame.open();
			if(saveGame.isSaved()){
				un_load_screen();
				Game a = new Game(canvas);
				a.loadGame();
				canvas.setScreen(a);
			}
			saveGame.close();
			break;
		case MenuScreen.HIGHSCORE:
			un_load_screen();
			canvas.setScreen(new HighScore(canvas));
			break;
		case MenuScreen.OPTIONS:
			//un_load_screen();
			canvas.setScreen(new Setting(canvas));
			break;
		case MenuScreen.ABOUT:
			un_load_screen();
			canvas.setScreen(new About(canvas));
			break;
		case MenuScreen.GUIDE:
			un_load_screen();
			canvas.setScreen(new Guide(canvas));
			break;
		case MenuScreen.EXIT:
			//un_load_screen();
			canvas.getMidletGame().destroyApp();
			break;

		default:
			break;
		}
	}
	public void positionY(){
		MenuScreen.curY = 60 + MenuScreen.currentMenu*25;
	}
	public void update() {
		// TODO Auto-generated method stub
		
	}

	public void paint(Graphics g) {
		g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
		g.drawImage(img[MenuScreen.MENU_BG], 0, 0, Graphics.LEFT | Graphics.TOP);
		g.drawImage(img[MenuScreen.MENU_LIST], 200, 60, Graphics.LEFT | Graphics.TOP);
		saveGame.open();
		if(!saveGame.isSaved()){
			g.drawImage(img[MenuScreen.MENU_CONTINUE], 200, 85, Graphics.LEFT | Graphics.TOP);
			
		}
		//g.drawImage(img[MenuScreen.MENU_OPTION], 200, 135, Graphics.LEFT | Graphics.TOP);
		saveGame.close();
		g.drawImage(img[MenuScreen.MENU_CHOISE], MenuScreen.curX, MenuScreen.curY, Graphics.LEFT | Graphics.TOP);
	}
	
}
